package com.bwie.goods.controller;

import com.alibaba.fastjson.JSONObject;
import com.bwie.common.domain.Goods;
import com.bwie.common.domain.request.GoodsReq;
import com.bwie.common.domain.response.GoodsQueryRes;
import com.bwie.common.domain.response.GoodsRes;
import com.bwie.common.result.PageResult;
import com.bwie.common.result.Result;
import com.bwie.goods.service.GoodsService;
import com.bwie.goods.util.OssUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/goods")
@Log4j2
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private HttpServletRequest request;

    /**
     * 根据编号查询
     * @param goodsId
     * @return
     */
    @GetMapping("/findById/{goodsId}")
    public Result findById(@PathVariable Integer goodsId){
        Goods goods = goodsService.findById(goodsId);
        return Result.success(goods);
    }

    /**
     * 查所有(es用)
     * @return
     */
    @GetMapping("/findAll")
    public Result<List<GoodsQueryRes>> findAll(){
        List<GoodsQueryRes> goodsQueryResList = goodsService.findAll();
        return Result.success(goodsQueryResList);
    }

    /**
     * 列表
     * @param goodsReq
     * @return
     */
    @PostMapping("/goodsList")
    public Result<PageResult<GoodsRes>> goodsList(@RequestBody GoodsReq goodsReq){
       log.info("功能：列表，请求方式：{}，请求路径：{}，请求参数：{}",request.getMethod(),request.getRequestURI(), JSONObject.toJSONString(goodsReq));
       PageHelper.startPage(goodsReq.getPageNum(),goodsReq.getPageSize());
       List<GoodsRes> goodsResList = goodsService.goodsList(goodsReq);
       PageInfo<GoodsRes> info = new PageInfo<>(goodsResList);
       Result<PageResult<GoodsRes>> result = PageResult.toResult(info.getTotal(), goodsResList);
       log.info("功能：列表，请求方式：{}，请求路径：{}，请求参数：{}",request.getMethod(),request.getRequestURI(),JSONObject.toJSONString(result));
       return result;
    }

    /**
     * 添加
     * @param goods
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody Goods goods){
        goodsService.insert(goods);
        return Result.success();
    }

    /**
     * 修改
     * @param goods
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Goods goods){
        goodsService.update(goods);
        return Result.success();
    }

    /**
     * 单删
     * @param goodsId
     * @return
     */
    @DeleteMapping("/del/{goodsId}")
    public Result del(@PathVariable Integer goodsId){
        goodsService.del(goodsId);
        return Result.success();
    }

    /**
     * 批删
     * @param goodsIds
     * @return
     */
    @DeleteMapping("/batchDel")
    public Result batchDel(@RequestBody List<Integer> goodsIds){
        goodsService.batchDel(goodsIds);
        return Result.success();
    }

    /**
     * 图片上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result<String> upload(@RequestParam("file")MultipartFile file){
        String filePath = OssUtil.uploadMultipartFile(file);
        return Result.success(filePath);
    }
}
